namespace stsdev.UnitTest
{
    using System;
    using NUnit.Framework;
    using stsdev;
    using System.IO;
    using System.Threading;

    [TestFixture]
    public class CoreUnitTest
    {
        string TESTDIRECTORY = "c:\\temp\\STSDEVTESTPROJECTS";

        [SetUp]
        public void Init()
        {
            if (Directory.Exists(TESTDIRECTORY))
                Directory.Delete(TESTDIRECTORY, true);

            Directory.CreateDirectory(TESTDIRECTORY);

            string KEYFILENAME = TESTDIRECTORY + "\\testkey.snk";
            StrongNameKeyBuilder.CreateNew(KEYFILENAME);
            SolutionBuilder.KeyFileSource = KEYFILENAME;
        }

        [Test]
        public void SolutionBuildTest()
        {
            for (int i = 0; i < SolutionBuilder.SolutionProviders.Count; i++)
            {

                SolutionBuilder.SolutionProvider = (ISolutionProvider)SolutionBuilder.SolutionProviders[i];
                Console.WriteLine(string.Format("{0}: {1} - {2}",i, SolutionBuilder.SolutionProvider.Title, SolutionBuilder.SolutionProvider.GetType().Name));

                switch (SolutionBuilder.SolutionProvider.Title)
                {
                    case "Simple Feature Solution (C# assembly)":
                        ((SimpleFeatureSolutionProvider)SolutionBuilder.SolutionProvider).ShowDialog = false;
                        ((SimpleFeatureSolutionProvider)SolutionBuilder.SolutionProvider).FeatureScope = FeatureScopeEnum.Site; //TODO: do forall FeatureScopes
                        ((SimpleFeatureSolutionProvider)SolutionBuilder.SolutionProvider).IncludeFeatureReceiver = true;//TODO: do for false
                        BuildSolution();
                        break;
                    default:
                        BuildSolution();
                        break;
                } 
            }
        }

        private static void ResetStaticSolutionBuilder()
        {
            SolutionBuilder.FullAssemblyName = null;
            SolutionBuilder.PrimaryFeatureName = null;
            SolutionBuilder.ResetWebServer = false;
            SolutionBuilder.SolutionId = null;
            SolutionBuilder.TargetPath = null;
            SolutionBuilder.TargetName = null;
            SolutionBuilder.FullAssemblyName = null;
            SolutionBuilder.PrimaryFeatureName = null;
            ProjectFileBuilder.SourceFiles.Clear();
            ProjectFileBuilder.Folders.Clear();
        }

        private void BuildSolution()
        {
            Array lstSolutionVersion = System.Enum.GetValues(typeof(SolutionVersionTypeEnum));
            foreach (SolutionVersionTypeEnum enSolutionVersionType in lstSolutionVersion)
            {
                ResetStaticSolutionBuilder();

                SolutionBuilder.SolutionName = string.Format("{0}.{1}", SolutionBuilder.SolutionProvider.Title, enSolutionVersionType.ToString());
                Console.WriteLine(SolutionBuilder.SolutionName);
                SolutionBuilder.SolutionDirectory = string.Format("{0}\\{1}", TESTDIRECTORY, SolutionBuilder.SolutionName);
                SolutionBuilder.SolutionVersionType = enSolutionVersionType;
                SolutionBuilder.InitializeSolution();//TODO: get STSDEV.exe location working not in temp location
                SolutionBuilder.SolutionProvider.AddSolutionItems();
                SolutionBuilder.CompleteSolution();

                //TODO: run through deploying wsps for each one to ensure packages work
            }
        }
    }
}